/**
 * @description 成功轻提示对话框
 * @param options 参数与wx.Toast保持一致
 * 
*/
const okToastApi = (options: any = {}) => {
  wx.showToast({
    title: '操作成功',
    icon: 'success',
    duration: 2000,
    mask: false,
    ...options
  })
}


/**
 * @description 失败轻提示对话框
 * @param options 参数与wx.Toast保持一致
 * 
*/

const errToastApi = (options: any = {}) => {
  wx.showToast({
    title: '操作失败',
    icon: 'error',
    duration: 2000,
    mask: false,
    ...options
  })
}


/**
 * @description 模态对话框
 * @param options 参数与wx.Model保持一致
 * 
*/

const modalApi = (options: any = {}) => {
  return new Promise((resolve, reject) => {
    wx.showModal({
      title: '提示',
      content: '您确定执行该操作吗？',
      confirm: '#3514g',
      ...options,
      complete(confirm:any, cancel:any) {
        confirm && resolve(true)
        cancel && resolve(false)
      },
    })
  })
}


// 存储数据 
const setStorageApi = (key: any, value: any) => {
  try {
    wx.setStorageSync(key, value)
  } catch (error) {
    console.log(`存储指定${key}数据发生了异常`, error)
  }
}

// 存储数据 
const getStorageApi = (key: any) => {
  try {
    const value: any = wx.getStorageSync(key)

    if (value) {
      return value
    }
  } catch (error) {
    console.log(`读取指定${key}数据发生了异常`, error)
  }
}

const removeStorageApi = (key: any) => {
  try {
    const value: any = wx.removeStorageSync(key)

    if (value) {
      return value
    }
  } catch (error) {
    console.log(`移除指定${key}数据发生了异常`, error)
  }
}

const clearStorageApi = () => {
  try {
    const value: any = wx.clearStorageSync()

    if (value) {
      return value
    }
  } catch (error) {
    console.log(`清除数据发生了异常`, error)
  }
}

// 存储数据 
const asyncSetStorageApi = (key: any, data: any) => {
  return new Promise((resolve: any, reject: any) => {
    try {
      wx.setStorage({
        key, data, complete(res) {
          resolve(res)
        }
      })
    } catch (error) {
      reject(error)
    }
  })

}

// 存储数据 
const asyncGetStorageApi = (key: any) => {

  return new Promise((resolve: any, reject: any) => {
    try {
      wx.getStorage({key,complete(res:any){
      resolve(res)
      }})
    } catch (error) {
      reject(error)
    }
  })
  
}

const asyncRemoveStorageApi = (key: any) => {

  return new Promise((resolve: any, reject: any) => {
    try {
      wx.removeStorage({key,complete(res:any){
      resolve(res)
      }})
    } catch (error) {
      reject(error)
    }
  })

 
}

const asyncClearStorageApi = () => {
  return new Promise((resolve: any, reject: any) => {
    try {
      wx.clearStorage({complete(res:any){
      resolve(res)
      }})
    } catch (error) {
      reject(error)
    }
  })
}


export default {
  okToastApi,
  errToastApi,
  modalApi,
  setStorageApi,
  getStorageApi,
  removeStorageApi,
  clearStorageApi,
  asyncSetStorageApi,
  asyncClearStorageApi,
  asyncRemoveStorageApi,
  asyncGetStorageApi
}